package org.example.cet.demos.controller;


import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.HomeworkForm;
import org.example.cet.demos.domain.po.Homework;
import org.example.cet.demos.domain.query.HomeworkQuery;
import org.example.cet.demos.service.impl.HomeworkServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/homework")
public class HomeworkController {
    @Autowired
    private HomeworkServiceImpl homeworkService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody HomeworkForm homeworkForm) {
        Homework homework = converter.ToHomework(homeworkForm);
        homework.setCreateTime(LocalDateTime.now());
        homeworkService.save(homework);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        homeworkService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable Long id, @Valid @RequestBody HomeworkForm homeworkForm) {
        Homework homework = converter.ToHomework(homeworkForm);
        homework.setId(id);
        homeworkService.updateById(homework);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(HomeworkQuery homeworkQuery) {
        PageBean pb = homeworkService.getPage(homeworkQuery);
        return Result.success(pb);
    }

    @GetMapping("/getList")
    public Result getList(){
        return Result.success(homeworkService.getList());
    }
}
